package com.liuqi.aiagent.handler;

import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.util.ResponseUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@ControllerAdvice
@Slf4j
public class WebExceptionHandler {

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public void httpMessageNotReadableException(HttpServletRequest request,
                                                HttpServletResponse response,
                                                HttpMessageNotReadableException exception) {
        log.error("全局捕获异常[HttpMessageNotReadableException]", exception);
        ResponseUtil.responseError(response, "系统服务异常");
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result resolveConstraintViolationException(Exception exception) {
        exception.printStackTrace();
        return ApiResult.fail("系统服务异常");
    }

}
